home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / tracker-4.13.lha / tracker / Arch / Aix / audio.c next >
Encoding:
C/C++ Source or Header  |  1995-02-08  |  4.3 KB  |  196 lines

  1. /* Aix/audio.c 
  2.     vi:ts=3 sw=3:
  3.  */
  4. /* Ported from Linux/audio.c by Sam Hartman <hartmans@mit.edu>*/
  5. /* minor mods for pl14 by Mike Battersby */
  6. /* Modified from soundblaster_audio.c by Hannu Savolainen */
  7. /* hsavolai@cs.helsinki.fi */
  8.  
  9. #include "defs.h"
  10. #include <unistd.h>
  11. #include <fcntl.h>
  12. #include "extern.h"
  13.  
  14. #undef SIGNED /*redefined in system include file*/
  15. #include <sys/audio.h>
  16. #include <sys/acpa.h>
  17.  
  18.  
  19. ID("$Id: audio.c,v 1.2 1995/02/01 16:43:47 espie Exp $")
  20.  
  21. LOCAL unsigned char *buffer;    /* buffer for ready-to-play samples */
  22. LOCAL unsigned short *buffer16;            /* Sure this isn't unsigned short ? */
  23. LOCAL int buf_index;               /* index conflicts with index(3) */
  24. LOCAL int buf_max;
  25. LOCAL int audio;               /* /dev/acpa0/1 */
  26.  
  27. LOCAL int stereo;                    /* are we playing stereo or not ? */
  28. /* 256th of primary/secondary source for that side. */
  29. LOCAL int primary=512, secondary=0;
  30. LOCAL int dsp_samplesize = 16; /* must be 8 or 16 */
  31.  
  32. void set_mix(percent)
  33. int percent;
  34.     {
  35.     percent *= 256;
  36.     percent /= 100;
  37.     primary = percent;
  38.     secondary = 512 - percent;
  39.     }
  40.  
  41. int open_audio(f, s)
  42. int f;
  43. int s;
  44.       {
  45.       audio_init init;
  46.     audio_control control;
  47.     audio_change change;
  48.     audio = open("/dev/acpa0/1", O_WRONLY, 0);
  49.    if (audio == -1)
  50.         end_all("Error opening audio device");
  51.  
  52.     if (f==0) f = 44100;
  53.  
  54.    init.srate = f;
  55.    init.bits_per_sample = 16;
  56.    init.mode = PCM;
  57.     init.channels = s?2:1;
  58.    init.flags = BIG_ENDIAN;
  59.     init.operation = PLAY;
  60.    if (ioctl(audio, AUDIO_INIT, &init)!= 0)
  61.         end_all("Error initializing ACPA");
  62.  
  63.     stereo = (init.channels == 2)?1:0;
  64.     buf_max = init.bsize*20;    /* This is set by the ioctl. */
  65.  
  66.    buffer = malloc(buf_max);
  67.    buffer16 = (short *)buffer;
  68.    buf_index = 0;
  69.  
  70.    control.ioctl_request = AUDIO_CHANGE;
  71.     control.request_info = &change;
  72.     control.position = 0;
  73.  
  74.     change.dev_info = 0;
  75.     change.input = AUDIO_IGNORE;
  76.     change.output = OUTPUT_1;
  77.     change.monitor = AUDIO_IGNORE;
  78.     change.volume = 0x7fff0000;
  79.     change.volume_delay = 0;
  80.     change.balance = 0x3fff0000;
  81.     change.balance_delay = 0;
  82.     change.treble = AUDIO_IGNORE;
  83.     change.bass = AUDIO_IGNORE;
  84.     change.pitch = AUDIO_IGNORE;
  85.     
  86.     if (ioctl(audio, AUDIO_CONTROL, &control) != 0)
  87.         end_all( "Error changing ACPA parameters");
  88.  
  89.     control.ioctl_request = AUDIO_START;
  90.  
  91.       if (ioctl(audio, AUDIO_CONTROL, &control) != 0)
  92.         end_all("Error starting ACPA");
  93.  
  94.       return init.srate;
  95.  
  96.    }
  97.  
  98. LOCAL void actually_flush_buffer()
  99.    {
  100.    int l,i;
  101.  
  102.    l = sizeof(*buffer) * buf_index;
  103.    if (dsp_samplesize !=8) l *= 2;
  104.    write(audio, buffer, l);
  105.  
  106.    buf_index = 0;
  107.    }
  108.  
  109. void output_samples(left, right)
  110. unsigned int left, right;
  111.    {
  112.    if (dsp_samplesize != 8)    /* Cool! 16 bits/sample */
  113.         {
  114.        if (stereo)
  115.            {
  116.           if (buf_index * 2 >= buf_max - 1) 
  117.               actually_flush_buffer();
  118.  
  119.           buffer16[buf_index++] = 
  120.                 32768+((left*primary + right*secondary) / 65536);
  121.           buffer16[buf_index++] = 
  122.                 32768+((right*primary + left*secondary) / 65536);
  123.               }
  124.          else
  125.            {
  126.           if (buf_index * 2 >= buf_max) 
  127.               actually_flush_buffer();
  128.               buffer16[buf_index++] = 32768+(left + right)/256;
  129.           }
  130.         }
  131.     else
  132.        {
  133.        if (stereo)
  134.            {
  135.           if (buf_index >= buf_max - 1) 
  136.                 actually_flush_buffer();
  137.           buffer[buf_index++] = ((left*primary + right*secondary) >> 24)
  138.               + 128;
  139.           buffer[buf_index++] = ((right*primary + left*secondary) >> 24)
  140.              + 128;
  141.           }
  142.        else
  143.            {
  144.           if (buf_index >= buf_max) 
  145.                 actually_flush_buffer();
  146.           buffer[buf_index++] = ((left + right) >> 16) + 128;
  147.           }
  148.        }
  149.     }
  150.  
  151. void flush_buffer()
  152.    {    /* Dummy version */
  153.    }
  154.  
  155. /* We must wait for all samples to be played before closing.*/
  156. void close_audio()
  157.    {
  158.    audio_control control;
  159.    if (buf_index != 0)
  160.         actually_flush_buffer();
  161.    ioctl(audio, AUDIO_WAIT, 0);
  162.    control.position = 0;
  163.     control.ioctl_request = AUDIO_STOP;
  164.     control.request_info = 0;
  165.     ioctl(audio, AUDIO_CONTROL, &control);
  166.  
  167.    close(audio);
  168.    free(buffer);
  169.    }
  170.  
  171. /* dummy system calls, to patch ? */
  172. void set_synchro(s)
  173.     {
  174.     }
  175.  
  176. int update_frequency()
  177.     {
  178.     return 0;
  179.     }
  180.  
  181. void discard_buffer()
  182.     {
  183.    audio_control control;
  184.    control.ioctl_request = AUDIO_STOP;
  185.     control.request_info = 0;
  186.     control.position = 0;
  187.     ioctl(audio, AUDIO_CONTROL, &control);
  188.    usleep(150000);
  189.    control.ioctl_request = AUDIO_START;
  190.    if (ioctl(audio, AUDIO_CONTROL, &control) == -1)
  191.         end_all ("Unable to restart AACPA");
  192.     buf_index = 0;
  193.     }
  194.  
  195.  
  196.